package com.vvv.designpattern.repo;

import com.vvv.designpattern.pojo.ProductItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

/**
 * @author vvv
 * @date 2023-11-18 15 45
 * discription
 */
@Repository
public interface ProductItemRepository extends JpaRepository<ProductItem, Integer> {
    /**
     * 向数据库添加新的商品类目
     * @param name
     * @param pid
     */
    @Modifying
    @Query(value="insert into product_item " +
                    "values((select max(id) + 1 from product_item), ?1, ?2)", nativeQuery = true)
    void addItem(String name, int pid);

    /**
     * 向数据库删除商品类目及其直接子类目
     * @param id
     */
    @Modifying
    @Query(value="delete from product_item where id=?1 or pid=?1", nativeQuery = true)
    void delItem(int id);

    ProductItem findByNameAndPid(String name, int pid);
}
